package com.example.spinning.service;

import com.example.spinning.bean.Module;
import com.example.spinning.bean.Operation;
import com.example.spinning.bean.Organ;
import com.example.spinning.bean.User;
import com.example.spinning.mapper.OrganMapper;
import com.example.spinning.mapper.PrivMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
/**
 * 权限
 */
@Service
@Transactional
public class PrivService {
    @Autowired
    PrivMapper privMapper;
    //模块
    public List<Module> getAllModules() {
        return privMapper.getAllModules();
    }

    public boolean deleteModuleByIds(String ids) {
        String[] split = ids.split(",");
        int result = privMapper.deleteModuleByIds(split);
        return result == split.length;
    }

    public int updateModuleById(Module module) {
        return privMapper.updateModuleById(module);
    }

    public int addModule(Module module) {
        return privMapper.addModule(module);
    }
    //操作
    public List<Operation> getAllOperations() {
        return privMapper.getAllOperations();
    }

    public boolean deleteOperationByIds(String ids) {
        String[] split = ids.split(",");
        int result = privMapper.deleteOperationByIds(split);
        return result == split.length;
    }

    public int updateOperationById(Operation operation) {
        return privMapper.updateOperationById(operation);
    }

    public int addOperation(Operation operation) {
        return privMapper.addOperation(operation);
    }

    //模块与操作
    //添加权限
    public int addPriv(String priv) {
        String[] split = priv.split("_");
        Module module = new Module();
        module.setModule(split[0]);
        module.setOperation(split[1]);
        Operation operation = new Operation();
        operation.setOperation(split[1]);
        int id1= privMapper.getModuleIdByName(split[0]);
        int id2=privMapper.getOperationIdByName(split[1]);


        List<Module> list = privMapper.getSpecificModules(id1,id2);
//        判断是否存在
        if(list.size()==0){
            List<Module> list1 = privMapper.getSpecificModule(module);
            if(list1.size()==0){
                privMapper.addModule(module);
                id1 = privMapper.getModuleIdByName(split[0]);
            }

            List<Operation> list2 = privMapper.getSpecificOperation(operation);
            if(list2.size()==0){
                privMapper.addOperation(operation);
                id2=privMapper.getOperationIdByName(split[1]);
            }
            int result=privMapper.addPriv(id1,id2);
            return result;
        }
        else{
            return 2;
        }

    }

    //修改权限
    public int updatePriv(Module module) {
        Operation operation = new Operation();
        operation.setOperation(module.getOperation());
        int id1= privMapper.getModuleIdByName(module.getModule());
        int id2=privMapper.getOperationIdByName(module.getOperation());
        //        判断是否存在

        List<Module> list1 = privMapper.getSpecificModule(module);
        if(list1.size()==0){
            privMapper.addModule(module);
            id1 = privMapper.getModuleIdByName(module.getModule());
        }

        List<Operation> list2 = privMapper.getSpecificOperation(operation);
        if(list2.size()==0){
            privMapper.addOperation(operation);
            id2=privMapper.getOperationIdByName(module.getOperation());
        }

        module.setModuleId(new Long(id1));
        module.setOperationId(new Long(id2));
//        判断修改后的权限是否存在
        List<Module> privList = privMapper.getSpecificModules(id1,id2);
        if(privList.size()>0){
            return 2;
        }
        int result=privMapper.updatePriv(module);
        return result;


    }
//    查询全部权限
    public List<Module> getAllPrivs() {
    return privMapper.getAllPrivs();
}

    public boolean deletePrivs(String ids) {
        String[] split = ids.split(",");


        int result = privMapper.deletePrivs(split);

        return result == split.length;
    }

//    按条件查询权限数量
public int getPrivCountByCondition(Module module) {
    return privMapper.getPrivCountByCondition(module);
}

    //    按条件查询权限
    public List<Module> getPrivByCondition(int page, int count, Module module) {
        int start = (page-1)*count;
        return privMapper.getPrivByCondition(start,count,module);
    }

    //    查询全部权限
    public List<Module> getPrivs(Module module) {
        return privMapper.getPrivs(module);
    }





}
